package com.csnf.servlet;

import com.csnf.dao.IClaDao;
import com.csnf.dao.IStudentsDao;
import com.csnf.dao.impl.ClaDaoImpl;
import com.csnf.dao.impl.StudentsDaoImpl;
import com.csnf.entity.Cla;
import com.csnf.entity.Students;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

@WebServlet({"/StudentServlet","/deleteById","/getStudentById","/addStudent"})
public class StudentServlet extends HttpServlet{
    IStudentsDao studentsDao = new StudentsDaoImpl();
    IClaDao claDao = new ClaDaoImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        switch (servletPath){
            case "/getStudentById":
                request.setAttribute("stu",studentsDao.findById(Integer.parseInt(request.getParameter("sId"))));
                request.setAttribute("claList",claDao.findAll());
                request.getRequestDispatcher("editStudent.jsp").forward(request,response);
                break;
            case "/StudentServlet":
                String sName=request.getParameter("sName");
                if(sName!=null){
                    request.setAttribute("stuList",studentsDao.findByName(sName));
                }else {
                    request.setAttribute("stuList",studentsDao.findAll());
                }
                request.getRequestDispatcher("student.jsp").forward(request,response);
                break;
            case "/deleteById":
                studentsDao.deleteById(Integer.parseInt(request.getParameter("id")));
                request.setAttribute("stuList",studentsDao.findAll());
                request.getRequestDispatcher("student.jsp").forward(request,response);
                break;
            default:
                    break;

        }
//        if (servletPath.equals("/deleteById")){
//            studentsDao.deleteById(Integer.parseInt(request.getParameter("id")));
//            request.setAttribute("stuList",studentsDao.findAll());
//            request.getRequestDispatcher("student.jsp").forward(request,response);
//        }else {
//            String sName=request.getParameter("sName");
//            if(sName!=null){
//                request.setAttribute("stuList",studentsDao.findByName(sName));
//            }else {
//                request.setAttribute("stuList",studentsDao.findAll());
//            }
//            request.getRequestDispatcher("student.jsp").forward(request,response);
//
//        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String servletPath = request.getServletPath();
        String sId = request.getParameter("sId");
        String sName = request.getParameter("sName");
        LocalDate birth = LocalDate.parse(request.getParameter("birth"));
        String gender = request.getParameter("gender");
        Integer claId = Integer.parseInt(request.getParameter("cId"));
        switch (servletPath){
            case "/addStudent":
                studentsDao.add(new Students(null,sName,birth,gender,claId));
                break;
            case "/StudentServlet":
                studentsDao.update(new Students(Integer.parseInt(sId),sName,birth,gender,claId));
                break;
        }
        request.setAttribute("stuList",studentsDao.findAll());
        request.getRequestDispatcher("student.jsp").forward(request,response);
    }
}
